import axios from 'axios';
import JSONBig from 'json-bigint';
// import router from '../router/index';
const $http = axios.create({
  baseURL: '/webapi',
  // baseURL: 'http://10.33.32.189:8556/api',
  // baseURL: '/api',
  withCredentials: false,
  timeout: 10000,
  transformResponse: [
    function (data) {
      // 对 data 进行任意转换处理
      try {
        return JSONBig.parse(data);
      } catch (err) {
        console.log(err);
        return data;
      }
    },
  ],
});

var CancelToken = axios.CancelToken;
$http.interceptors.request.use((config) => {
  //axios无法配置跨域
  // config.headers['Access-Control-Allow-Origin'] = '*';
  //   config.headers['Content-Type'] = 'text/html; charset=UTF8';
   let cancel
  config.cancelToken = new CancelToken(function executor(c) {
    cancel = c
  })
  return config;
});

$http.interceptors.response.use(
  (respose) => {
    if (respose.status === 200) {
      // return respose.data;
      return respose.data;
    } else {
      alert('访问失效');
    }
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

export default $http;
